<template>
  <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px" class="demo-ruleForm login-container">
    <h3 class="title">密码找回</h3>
    <el-form-item prop="user">
      <el-col :span="15">
      	<el-input type="text" v-model="ruleForm2.user" auto-complete="off" placeholder="手机号"></el-input>
      </el-col>
      <el-col :span="6" :offset="2">
    	  <el-button type="primary" :loading="logining" v-if="!checked"  v-on:click="settime">发送验证码</el-button>
    	  <el-button type="primary" :loading="logining" :disabled="true" v-if="checked">{{countdown}}s后重新发送</el-button>
      </el-col>      
    </el-form-item>
    <el-form-item prop="rule">
      <el-col :span="20">
      	<el-input type="text" v-model="ruleForm2.rule" v-on:blur="rulenumber" auto-complete="off" placeholder="请输入验证码"></el-input>
      </el-col>
      <el-col :span="4">
    	  <p class="el-icon-circle-check fr " style="color:#13CE66;" v-if="ruleVisible"></p>
    	  
      </el-col>      
    </el-form-item>    
    <el-form-item prop="password">
      <el-input type="password" v-model="ruleForm2.password" auto-complete="off" placeholder="请输入新的密码"></el-input>
    </el-form-item>
    <el-form-item prop="aginpassword" style="margin: 0;">
      <el-input type="password" v-model="ruleForm2.aginpassword" auto-complete="off" placeholder="请再次输入新的密码"></el-input>
    </el-form-item>    
		<a  class="link" v-on:click="skip('/login')">返回登录</a>    
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:100%;" @click.native.prevent="addSubmit" :loading="logining">重置密码</el-button>
      <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
    </el-form-item>
  </el-form>
</template>

<script>
  import { requestLogin,requestLogins,usernamerule,rulepass,sendCode,resetPassword} from '../api/api';
  import ComFunJS from'../common/js/util'
  //import NProgress from 'nprogress'
  export default {
    data() {
			var rulesphone=(rules,value,callback)=>{
				let params={username:this.ruleForm2.user};
				usernamerule(params).then((r)=>{
					if(r){
						callback(new Error('手机号不存在'))
					}else{
						callback()
					}
				})
			}	   
			var phonerule=(rule, value, callback)=>{
	  			var myreg=/^[1][3,6,4,5,7,8][0-9]{9}$/;
	            if (!myreg.test(value)) {
	                 callback(new Error('请填写有效手机号'))
	            } else {
	                callback()
	            }	  
			}					
			var passwordrule=(rules,value,callback)=>{
				if(this.ruleForm2.password!=this.ruleForm2.aginpassword){
					callback(new Error('两次密码输入不同~'))					
				}else{
					callback()
				}
			}	  			
      return {
        logining: false,
        ruleForm2: {
          user: '',
          rule:'',
          password: '',
          aginpassword:""
        },
        router:[],
        ruleVisible:false,
        rules2:{
          user: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { validator: rulesphone,trigger: 'blur'},
            { validator: phonerule,trigger: 'blur'}
          ],
          rule: [
            { required: true, message: '请输入验证码', trigger: 'blur' },
          ],          
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ],
          aginpassword: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { validator:passwordrule,trigger: 'blur'}
          ]          
        },
        checked: false,
        countdown:60,
      };
    },
    methods: {
	    skip(link,id,row){
	        this.$router.push(
	        	{
	        		path:link,
	        		query:{
	        			id:id,
	        		}
	        	}
	        )
	    },	
	    rulenumber:function(){
				let params={phone:this.ruleForm2.user,code:this.ruleForm2.rule};  	
				rulepass(params).then((r)=>{
					this.ruleVisible=r
				})	    	
	   },
			settime(val) {
				var _this = this;
				if(this.ruleForm2.user==''){
	        this.$message({
	          message: '手机号不能为空',
	          type: 'error'
	        });					
	        return
				}
				if(!_this.checked){
					let params={phone:this.ruleForm2.user};  	
					sendCode(params)							
				}
				if(_this.countdown!=0){
	    		_this.checked=true;
	        _this.countdown--;
	        setTimeout(function() {
	            _this.settime()
	        },1000) 					
				}else{
					_this.checked=false;
					_this.countdown=60;			
				}
				
			},	    
			addSubmit: function () {	
				var _this=this;
				if(!_this.ruleVisible){
						_this.$message({
							message: '验证码不正确',
							type: 'error'
						});
						return
				}
				let param={password:_this.ruleForm2.password,phone:_this.ruleForm2.user};
				this.$refs.ruleForm2.validate((valid) => {
					if (valid) {
						this.$confirm('确认修改密码吗？', '提示', {}).then(() => {
							this.addLoading = true;
							resetPassword(param).then((r) => {
								if(!r.error){
									this.addLoading = false;
									this.$message({
										message: '重置密码成功',
										type: 'success'
									});
									_this.skip('/login')
								}else{
									this.addLoading = false;
									this.$message({
										message: '重置密码失败',
										type: 'error'
									});										
								}
							})
						});
					}
				});
			},	
    },
    mounted:function(){
    }
  }

</script>

<style lang="scss" scoped>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
    .link{
    	text-align:right;margin: 20px 0;display: inline-block;cursor: pointer;width: 100%;color: #1f2d3d;
    }
    .link:hover{
    	color: #20a0ff;
    	
    }    
  }
</style>